package com.prp.gwt.server;

import java.util.Set;

import com.prp.bo.Friend;
import com.prp.bo.User;
import com.prp.gwt.client.ClientException;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.user.friend.FriendService;
import com.prp.gwt.client.vo.UserList;

public class FriendServiceImpl extends GlobalServiceImpl implements
		FriendService {

	private static final long serialVersionUID = -5190307560080212417L;

	@Override
	public UserList getFriendList(int id) throws ClientException {
		validateAuth(ClientConstants.USER);
		UserList userList;
		int sender = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		if (id == -1)
			userList = Friend.getFriends(sender);
		else if (!com.prp.bo.User.hasUser(id))
			return null;
		else
			userList = Friend.getFriends(id);
		userList.setSender(User.parse(User.getUser(sender)));
		return userList;
	}

	@Override
	public boolean isNeighbour(int id1, int id2) throws ClientException {
		validateAuth(ClientConstants.USER);
		return Friend.isNeighbour(id1, id2);
	}

	@Override
	public void breakup(int id1, int id2) throws ClientException {
		validateAuth(ClientConstants.USER);
		int id = (Integer) getThreadLocalRequest().getSession().getAttribute(
				ClientConstants.ATTR_USER);
		if (!User.hasUser(id1) || !User.hasUser(id2))
			return;
		if (id == id1 || id == id2)
			Friend.breakup(id1, id2);
		else {
			validateAuth(ClientConstants.ADMIN);
			Friend.breakup(id1, id2);
		}
	}

	@Override
	public boolean invite(int toId) throws ClientException {
		validateAuth(ClientConstants.USER);
		int fromId = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		if (!User.hasUser(toId)
				|| !User.isActivated(User.getUser(toId).getEmail()))
			throw new ClientException("无法发送邀请", "用户"
					+ User.getUser(toId).getEmail() + "不存在<br/>请您刷新后再试");
		return Friend.invite(fromId, toId);
	}

	@Override
	public UserList findInvite() throws ClientException {
		validateAuth(ClientConstants.USER);
		int id = (Integer) getThreadLocalRequest().getSession().getAttribute(
				ClientConstants.ATTR_USER);
		UserList userList;
		userList = Friend.findInvite(id);
		userList.setSender(User.parse(User.getUser(id)));
		return userList;
	}

	public void cancelInvite(int fromId) throws ClientException{
		validateAuth(ClientConstants.USER);
		int toId = (Integer) getThreadLocalRequest().getSession().getAttribute(
				ClientConstants.ATTR_USER);
		Friend.cancelInvite(fromId, toId);
	}

	@Override
	public boolean buildup(int fromId) throws ClientException {
		validateAuth(ClientConstants.USER);
		int toId = (Integer) getThreadLocalRequest().getSession().getAttribute(
				ClientConstants.ATTR_USER);
		return Friend.buildup(fromId, toId);
	}

	@Override
	public UserList searchUser(String userName) throws ClientException {
		validateAuth(ClientConstants.USER);
		UserList userList;
		int sender = (Integer) getThreadLocalRequest().getSession()
				.getAttribute(ClientConstants.ATTR_USER);
		userList = Friend.searchByUserName(userName);
		userList.setSender(User.parse(User.getUser(sender)));
		return userList;
	}

	@Override
	public void deleteRecommend(int id) throws ClientException {
		validateAuth(ClientConstants.USER);
		Friend.deleteRecommend(id);
	}

	@Override
	public UserList getRecommendList() throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession().getAttribute(
				ClientConstants.ATTR_USER);
		return Friend.getRecommendList(userId);
	}

	@Override
	public void recommend(Set<Integer> fromIds,
			Set<Integer> toIds) throws ClientException {
		validateAuth(ClientConstants.USER);
		int userId = (Integer) getThreadLocalRequest().getSession().getAttribute(
				ClientConstants.ATTR_USER);
		Friend.recommend(userId, fromIds, toIds);
	}
	
}
